﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using CarparkIMS.Models;
using CarparkIMS_Model;
using Microsoft.AspNetCore.Components.Forms;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Data.SqlClient;
using Microsoft.EntityFrameworkCore;


namespace CarparkIMS.Controllers
{
    
    public class PlateRecognizeController : Controller
    {
        private readonly IMSContext _context;
        public PlateRecognizeController(IMSContext iMSContext)
        {
            _context = iMSContext;
        }

        //[Route("UnlockDoor")]
        //public IActionResult UnlockDoor()
        //{
        //    int doorid = 2;
        //    var doorstatus = _context.tbDoorStatus.Find(doorid);
        //    doorstatus.SendUnlock = true;
        //    _context.Entry(doorstatus).State = Microsoft.EntityFrameworkCore.EntityState.Modified;
        //    _context.SaveChanges();
        //    return Json(new { status = 1});
        //}
        [HttpPost]
        [Route("PlateReturn")]
        public IActionResult PlateReturn([FromBody] VAXTORJson model)
        {
            likonPlateLog log = new likonPlateLog();
           
            log.ip = model.ip;
            log.plate = model.plate;
            log.confidence = 0;// model.confidence;
            log.createdOn = DateTime.Now;
            log.date = model.date;
            log.OccuredDatetime = DateTime.Now;  //temp
            log.image64 = model.image64;
            _context.Add<likonPlateLog>(log);
            _context.SaveChanges();

            var mapCard = _context.likonPlateMapping.Where(a => !a.Deleted && a.PlateNo == model.plate).FirstOrDefault();
            if (mapCard != null)
            {
                String searchUserQ = "SELECT TOP 1 u.[UserID],u.[Name], u.[CardID] FROM dbo.tbUser u  WHERE RIGHT(N'0000000000000000'+u.[CardID],16) = RIGHT(N'0000000000000000'+@cardID,16)";

                var user = _context.tbUser.FromSqlRaw(searchUserQ, new SqlParameter("@cardID", mapCard.CardID)).FirstOrDefault();
                if (user != null)
                {
                    var mapDoor = _context.likonCameraDoorMapping.Where(a => !a.Deleted && a.CameraIP == model.ip).FirstOrDefault();
                    if (mapDoor != null)
                    {
                        int doorid = mapDoor.DoorID;
                        var panel = _context.tbDoor.Find(doorid)?.PanelID;
                        if (panel.HasValue)
                        {
                            tbTransaction t = new tbTransaction();
                            t.CardID = mapCard.CardID;
                            t.DoorID = doorid;
                            t.PanelID = panel.Value;
                            t.TransactionDate = log.createdOn;
                            t.TransactionLog = "";
                            t.TransactionTypeID = mapDoor.TransactionTypeID;
                            t.UserID = user.UserID;
                            _context.tbTransaction.Add(t);
                            _context.SaveChanges();
                            tbDisplayTransaction t2 = new tbDisplayTransaction();
                            t2.CardID = mapCard.CardID;
                            t2.DoorID = doorid;
                            t2.PanelID = panel.Value;
                            t2.TransactionDate = log.createdOn;
                            t2.TransactionLog = "";
                            t2.TransactionTypeID = mapDoor.TransactionTypeID;
                            t2.UserID = user.UserID;
                            _context.tbDisplayTransaction.Add(t2);
                            _context.SaveChanges();

                            var doorstatus = _context.tbDoorStatus.Find(doorid);
                            doorstatus.SendUnlock = true;
                            _context.Entry(doorstatus).State = Microsoft.EntityFrameworkCore.EntityState.Modified;
                            _context.SaveChanges();
                        }
                    }
                }

            }


            return Json(new { status = 1 });
        }

        //[Route("PlateList")]
        //public async Task<IActionResult> PlateList()
        //{
        //    var model = await _context.likonPlateMapping.Where(a => !a.Deleted).ToListAsync();
        //    return View(model);
        //}

        //[Route("PlateRegister")]
        //[HttpGet]
        //public IActionResult PlateRegister()
        //{
        //    PlateRegisterModel model = new PlateRegisterModel();
        //    return View(model);
        //}

        //[Route("PlateRegister")]
        //[HttpPost]
        //public IActionResult PlateRegister(PlateRegisterModel model)
        //{
        //    return View();
        //}

        //[Route("PlateDelete")]
        //[HttpPost]
        //public IActionResult PlateDelete(int Id)
        //{
        //    return View();
        //}

        [Route("Test")]
        public IActionResult Test()
        {
            likonPlateLog log = new likonPlateLog();
            log.ip = "1";
            log.plate = "2";
            log.confidence = 0;// model.confidence;
            log.createdOn = DateTime.Now;
            log.date = "3";
            _context.Set<likonPlateLog>().Add(log);
            _context.SaveChanges();

            var logs = _context.Set<likonPlateLog>().ToList();

            return Json(new { status = 1});

        }
    }
}
